package com.zhu.tool_test.starke.matrix.multipart.wrapper;


import com.zhu.tool_test.starke.matrix.multipart.core.MultipartData;
import com.zhu.tool_test.starke.util.buffer.core.StarkeBuilder;
import com.zhu.tool_test.starke.util.lang.Strings;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashMap;

/**
 * @author 4everlynn
 * @version V1.0
 * @description
 * @date 2019-04-28
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class DataWrapper extends HashMap<String, String>{
    private WrapperStrategy strategy;

    /**
     * 映射
     *
     * @param current 当前
     * @param target  目标
     * @return 自身
     */
    public DataWrapper map(String current, String target) {
        this.map(current, target, null);
        return this;
    }

    /**
     * 深层级的key替换
     *
     * @param target  目标
     * @param current 层级导航
     * @param scope   范围
     * @return 自身
     */
    public DataWrapper map(String current, String target, String scope) {
        if (null != strategy) {
            target = strategy.previous(target);
        }
        if (!Strings.isNullOrEmpty(scope)) {
            target = target.concat(StarkeBuilder
                    .build(",{}", scope));
        }
        this.put(current, target);
        return this;
    }

    /**
     * 从MultipartData中获得key
     *
     * @param multipartData 目标
     * @return 自身
     */
    public DataWrapper from(MultipartData multipartData) {
        for (String key : multipartData.keySet()) {
            this.map(key, key);
        }
        return this;
    }

}
